
import numpy as np
import model
import pandas as pd
class Solver:
    def __init__(self,save_path=None) -> None:
        self.mnist=model.Mnist("/remote-home/liwei/神经网络张力/mytest/data/mnist_all.mat")
        self.save_path=save_path
    def find_hidden(self,hidden,epoch,batch_size,lr,lamdb):
    # hidden:候选参数
        ans=[]
        # max_train_acc,max_train_loss,max_test_acc,max_test_loss=0,0,0,0
        for item in hidden:
            print('现在进行的网络架构为'+str(item))
            self.mnist.initParam(hidden=item)
            train_acc,train_loss,test_acc,test_loss=self.mnist.train(epoch, batch_size, lr,lamdb)
            ans.append([item,train_acc,train_loss,test_acc,test_loss])
        return ans
    def find_hyper_par(self,hidden,epoch,batch_size,lr,lamdb):
        hyper_par=[]
        # max_train_acc,max_train_loss,max_test_acc,max_test_loss=0,0,0,0
        for lr0 in lr:
            for lamdb0 in lamdb:
                print('现在验证的超参数lr为:'+str(lr0)+" lamda:",str(lamdb0))
                self.mnist.initParam(hidden)
                train_acc,train_loss,test_acc,test_loss=self.mnist.train(epoch, batch_size, lr0,lamdb0)
                hyper_par.append([lr0,lamdb0,train_acc,train_loss,test_acc,test_loss])
        return hyper_par
    

    def find_batch(self,hidden,epoch,batch_size,lr,lamdb):
    # hidden:候选参数
        batch=[]
        # max_train_acc,max_train_loss,max_test_acc,max_test_loss=0,0,0,0
        for item in batch_size:
            print('现在进行的batch_size为'+str(item))
            self.mnist.initParam(hidden)
            train_acc,train_loss,test_acc,test_loss=self.mnist.train(epoch, item, lr,lamdb)
            batch.append([item,train_acc,train_loss,test_acc,test_loss])
        return batch
    
    def find_epoch(self,hidden,epoch,batch_size,lr,lamdb):
    # hidden:候选参数
        # max_train_acc,max_train_loss,max_test_acc,max_test_loss=0,0,0,0
        self.mnist.initParam(hidden)
        self.mnist.train(epoch, batch_size, lr,lamdb)
        self.mnist.plot()
        return 

if __name__ == '__main__':
    # epoch = 50
    # lr = 0.01
    # batch_size = 128
    # lamdb= 0.01
    # 调整隐藏层大小
    # hidden0=[]
    # for i in range(3,11):hidden0.append(2**i)
    # print(hidden0)

    solver=Solver()
    # ans=solver.find_hidden(hidden=hidden0,epoch=10,batch_size=128,lr=0.01,lamdb=0.01)
    # pd_ans=pd.DataFrame(ans,columns=["hidden","train_acc","train_loss","test_acc","test_loss"])
    # print(pd_ans)

    # # 固定隐藏层
    # lr=[0.1**i for i in range(1,8)]
    # lamdb=[0.1**i for i in range(1,8)]
    # hyper_par=solver.find_hyper_par(hidden=512,epoch=10,batch_size=128,lr=lr,lamdb=lamdb)
    # pd_hyper=pd.DataFrame(hyper_par,columns=["lr","lamdb","train_acc","train_loss","test_acc","test_loss"])
    # print(pd_hyper)

    # batch_size
    # batch_size0=[2**i for i in range(3,11)]
    # batch=solver.find_batch(hidden=512,epoch=10,batch_size=batch_size0,lr=0.1,lamdb=0.00001)
    # pd_batch=pd.DataFrame(batch,columns=["batch_size","train_acc","train_loss","test_acc","test_loss"])
    # print(pd_batch)

    # epoch
    solver.find_epoch(hidden=512,epoch=25,batch_size=16,lr=0.1,lamdb=0.00001)


            
            


            




